<?php

namespace App\Admin\Controllers;

use App\Models\ShopKickback;
use App\Models\Shop;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\MessageBag;

class ShopKickbackController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(ShopKickback::class, function (Grid $grid) {

            $grid->filter(function($filter) {
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->like('shop.shop_name', '店铺名');
            });
            $grid->id('ID')->sortable();
            $grid->column('shop.shop_name', '店铺名');
            $grid->column('discount', '回扣')->display(function ($discount){
                return "<span class='badge bg-teal'>$discount</span>";
            });

            $grid->created_at('创建时间');
            $grid->updated_at('更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(ShopKickback::class, function (Form $form) {

            if(!request()->route()->parameters()) {
                $form->saving(function (Form $form) {
                    $shop_id = $form->shop_id;
                    for ($i = 0; $i < count($shop_id) - 1; $i++) {
                        $ShopKickback = ShopKickback::where('shop_id', $shop_id[$i])->first();
                        if ($ShopKickback) {
                            $shop = Shop::where('id', $shop_id[$i])->first();
                            $error = new MessageBag([
                                'title' => "《" . $shop['shop_name'] . '》已设置回扣',
                                // 'message' => 'message....',
                            ]);
                            return back()->with(compact('error'));
                        }
                        ShopKickback::create([
                            'shop_id' => $shop_id[$i],
                            'discount' => $form->discount,
                        ]);
                    }
                    echo "<script> alert('保存成功！');history.go(-2); </script>";
                    die;
                });
            }


            if(request()->route()->parameters()){
                $shop_id = ShopKickback::where('id',request()->route()->parameters())->first()->shop_id;
                $shop_name = Shop::where('id',$shop_id)->first()->shop_name;
                $form->text('shop.shop_name', '店铺名')->value($shop_name);
            }else {
                $data = Shop::all();
                $res = array();
                foreach ($data as $key => $value) {
                    $res[$value['id']] = $value['shop_name'];
                }
                $form->listbox('shop_id', '店铺名')->options($res);
            }
            $form->currency('discount', '回扣')->symbol('%')->rules('required');
            $form->display('created_at', '创建时间');
            $form->display('updated_at', '更新时间');
        });
    }
}
